﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager : MonoBehaviour {

    private GameObject m_startUI;
    private GameObject m_gameUI;
    private GameObject m_shopUI;

    private UILabel m_scoreLabel;
    private UILabel m_gemLabel;
    private UILabel m_gameScoreLabel;
    private UILabel m_gameGemLabel;
    private UILabel m_shopGemLabel;

    private GameObject m_playButton;
    private GameObject m_shopButton;
    private GameObject m_continueButton;
    private GameObject m_pauseButton;
    private GameObject m_buyButton;
    private GameObject m_shopPlayButton;

    private PlayerController m_playerController;
    private ShopManager m_shopManager;
    
	void Awake () {

        m_playerController = GameObject.Find("Player").GetComponent<PlayerController>();
        m_shopManager = GetComponent<ShopManager>();

        m_startUI = GameObject.Find("Start_UI");
        m_gameUI = GameObject.Find("Game_UI");
        m_shopUI = GameObject.Find("Shop_UI");

        m_scoreLabel = GameObject.Find("Score_Label").GetComponent<UILabel>();
        m_gemLabel = GameObject.Find("Gem_Label").GetComponent<UILabel>();
        m_gameScoreLabel = GameObject.Find("Game_Score_Label").GetComponent<UILabel>();
        m_gameGemLabel = GameObject.Find("Game_Gem_Label").GetComponent<UILabel>();
        m_shopGemLabel = GameObject.Find("Shop_Gem_Label").GetComponent<UILabel>();

        m_playButton = GameObject.Find("play_btn");
        UIEventListener.Get(m_playButton).onClick = PlayButtonClick;
        m_shopButton = GameObject.Find("shop_btn");
        UIEventListener.Get(m_shopButton).onClick = ShopButtonClick;//需要修改
        m_continueButton = GameObject.Find("game_play_btn");
        UIEventListener.Get(m_continueButton).onClick = PlayButtonClick;
        m_pauseButton = GameObject.Find("pause_btn");
        UIEventListener.Get(m_pauseButton).onClick = PauseButtonClick;
        m_shopPlayButton = GameObject.Find("shop_play_btn");
        UIEventListener.Get(m_shopPlayButton).onClick = ShopPlayButtonClick;//需要修改
        m_buyButton = GameObject.Find("buy_btn");
        UIEventListener.Get(m_buyButton).onClick = BuyButtonClick;//需要修改
    }

    private void Start()
    {
        Init();
    }

    /// <summary>
    /// 游戏开始的初始化
    /// </summary>
    private void Init()
    {
        //startUI
        m_scoreLabel.text = PlayerPrefs.GetInt("score", 0)+"";
        m_gemLabel.text = PlayerPrefs.GetInt("gem", 0) + "/100";

        //gameUI
        m_gameScoreLabel.text = 0 + "";
        m_gameGemLabel.text = m_gemLabel.text;
        //隐藏gameUI
        m_gameUI.SetActive(false);

        //shopUI
        m_shopGemLabel.text = m_gemLabel.text;
        //隐藏shopUI
        m_shopUI.SetActive(false);
    }

    /// <summary>
    /// 刷新分数和宝石的UI
    /// </summary>
    /// <param name="score"></param>
    /// <param name="gem"></param>
    public void UpdateUI(int score, int gem)
    {
        m_gemLabel.text = gem + "/100";
        m_gameGemLabel.text = gem + "/100";
        m_shopGemLabel.text = gem + "/100";
        m_gameScoreLabel.text = score + "";
    }

    private void PlayButtonClick(GameObject go)
    {
        StartGame();
    }

    private void PauseButtonClick(GameObject go)
    {
        PauseGame();
    }

    private void ShopButtonClick(GameObject go)
    {
        m_startUI.SetActive(false);
        m_shopUI.SetActive(true);
        m_playerController.Switch();
        m_shopManager.EnterShop();

    }

    private void ShopPlayButtonClick(GameObject go)
    {
        m_startUI.SetActive(true);
        m_shopUI.SetActive(false);
        m_playerController.Switch();
        m_shopManager.ExitShop();
        m_playerController.StartCoroutine("ResetGame", 0f);
    }

    private void BuyButtonClick(GameObject go)
    {
        if (m_playerController.GetGem() >= 100)
        {
            m_playerController.Pay();
            m_shopManager.Buy();
        }
    }

    public void StartGame()
    {
        m_startUI.SetActive(false);
        m_gameUI.SetActive(true);
        m_continueButton.SetActive(false);
        m_pauseButton.SetActive(true);
        m_playerController.StartGame();
    }

    public void PauseGame()
    {
        m_continueButton.SetActive(true);
        m_pauseButton.SetActive(false);
        m_playerController.PauseGame();
    }

    /// <summary>
    /// UI恢复到Start
    /// </summary>
    public void ResetUI()
    {
        m_scoreLabel.text = PlayerPrefs.GetInt("score").ToString();
        m_gameScoreLabel.text = 0 + "";
        m_gameUI.SetActive(false);
        m_startUI.SetActive(true);
    }
}
